﻿using Account.Model;
using DataAccessLibrary;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// https://go.microsoft.com/fwlink/?LinkId=234238 上介绍了“空白页”项模板

namespace Account
{
    /// <summary>
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// </summary>
    public sealed partial class Details : Page
    {
        private static string[] weeks = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };

        public Details()
        {
            this.InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            var groupedRecords = new ObservableCollection<GroupedRecord>();
            this.GroupedRecords.Source = groupedRecords;
            var data = DataAccess.GetData("select * from ConsumingRecords order by cdate desc");
            var gData = from d in data
                        group d by d.Date.Date;
            foreach (var d in gData)
            {
                var groupedRecord = new GroupedRecord()
                {
                    Date = d.Key.ToString("MM月dd日"),
                    Week = weeks[Convert.ToInt16(d.Key.DayOfWeek)],
                    ConsumeRecords = d.ToList().ConvertAll((DataAccessLibrary.ConsumeRecord a) =>
                    {
                        return (Model.ConsumeRecord)a;
                    })
                };
                groupedRecord.Spending = groupedRecord.ConsumeRecords.Sum((record) => { return record.Value; }).ToString();
                groupedRecords.Add(groupedRecord);
            }
        }
    }
}
